import 'package:flutter/material.dart';
import 'package:flutter_study/api/api_service.dart';
import 'package:flutter_study/model/category_model.dart';
import 'package:flutter_study/util/toast_util.dart';

class CategoryPageModel with ChangeNotifier {
  List<CategoryModel> list = [];
  bool loading = true;
  bool error = false;

  retry() {
    loading = true;
    notifyListeners();
    loadData();
  }

  void loadData() async {
    ApiService.getData(ApiService.category_url, success: (response) {
      List responseList = response as List;
      List<CategoryModel> categoryList = responseList
          .map((model) => CategoryModel.fromJson(model))
          .toList();
      this.list = categoryList;
      loading = false;
      error = false;
    }, fail: (e) {
      ToastUtil.showError(e.toString());
      loading = false;
      error = true;
    }, complete: () {
      notifyListeners();
    });
  }
}
